USE [PeDALS_DEV]
GO
/****** Object:  StoredProcedure [dbo].[PDL_RejectSeriesAcquisition]    Script Date: 01/06/2010 16:12:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PDL_RejectSeriesAcquisition]
  @SeriesId INT,
  @SeriesAcquisitionId INT
AS
BEGIN
	BEGIN TRANSACTION

	SET NOCOUNT ON

	BEGIN TRY
		CREATE TABLE #ItemIds (ItemId INT)

		INSERT INTO #ItemIds
		SELECT ItemId
		FROM [PeDALS_DEV].[dbo].[SeriesAcquisitionItem_LINK]
		WHERE SeriesId = @SeriesId
			AND SeriesAcquisitionId = @SeriesAcquisitionId

		DELETE FROM [PeDALS_DEV].[dbo].[SeriesAcquisitionItem_LINK]
		WHERE SeriesId = @SeriesId AND SeriesAcquisitionId = @SeriesAcquisitionId

		DELETE FROM [PeDALS_DEV].[dbo].[ItemGeoLocation_LINK]
		WHERE ItemId IN (SELECT ItemId FROM #ItemIds)

		DELETE FROM [PeDALS_DEV].[dbo].[ItemDate]
		WHERE ItemId IN (SELECT ItemId FROM #ItemIds)

		DELETE FROM [PeDALS_DEV].[dbo].[ItemKeyword]
		WHERE ItemId IN (SELECT ItemId FROM #ItemIds)

		DELETE FROM [PeDALS_DEV].[dbo].[ItemParty]
		WHERE ItemId IN (SELECT ItemId FROM #ItemIds)

		DELETE FROM [PeDALS_DEV].[dbo].[Component]
		WHERE ItemId IN (SELECT ItemId FROM #ItemIds)

		DELETE FROM [PeDALS_DEV].[dbo].[Item]
		WHERE ItemId IN (SELECT ItemId FROM #ItemIds)

		UPDATE [PeDALS_DEV].[dbo].[SeriesAcquisition]
		SET SeriesAcquisitionReadyToIngest = 3 -- reprocess
		WHERE SeriesId = @SeriesId
			AND SeriesAcquisitionId = @SeriesAcquisitionId

		DROP TABLE #ItemIds

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		ROLLBACK TRANSACTION

		EXEC dbo.PDL_RETHROWERROR
	END CATCH
END

GO